package com.datamation.swadeshi.view;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.SearchView.OnQueryTextListener;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.adapter.SetTitleAdapter;
import com.datamation.swadeshi.adapter.TestingAdapter;
import com.datamation.swadeshi.adapter.VanInvoiceHistoryAdapter;
import com.datamation.swadeshi.control.VanSalePrintPreviewAlertBox;
import com.datamation.swadeshi.control.data.InvDetDS;
import com.datamation.swadeshi.control.data.InvHedDS;
import com.datamation.swadeshi.control.data.ItemLocDS;
import com.datamation.swadeshi.model.InvHed;
import com.datamation.swadeshi.model.ItemLoc;

public class FragmentTestingRND extends Fragment implements OnClickListener{
	View view;
	
	ListView lvInvoiceList;
	
	//InvoiceDS invoiceDS;
	//InvoiceItemAdapter invoiceItemAdapter;
	ArrayList<ItemLoc> arrayList;
	//Customer nearbyCustomer;
	
	@Override	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		view = inflater.inflate(R.layout.testing, container, false);
		
		lvInvoiceList = (ListView) view.findViewById(R.id.listView1);
		Button update = (Button) view.findViewById(R.id.button1);
		//registerForContextMenu(lvInvoiceList);
		
		getActivity().getActionBar().removeAllTabs();
		getActivity().getActionBar().setNavigationMode(0);
		
		setHasOptionsMenu(true);
       
		SetTitleAdapter adapter =new SetTitleAdapter();
		adapter.createCutomActionBarTitle("Testing ", getActivity());
		
		//display
		displayInvoiceHistory();
	

		update.setOnClickListener(this);
		
		//back button disable 
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        view.setOnKeyListener(new View.OnKeyListener() {
		        @Override
		        public boolean onKey(View v, int keyCode, KeyEvent event) {
		            Log.i("", "keyCode: " + keyCode);
		            if( keyCode == KeyEvent.KEYCODE_BACK ) {
		            	
		            	Toast.makeText(getActivity(),"Back Navigationid is disable", Toast.LENGTH_SHORT).show();
		               // getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
		                return true;
		            }else if ((keyCode == KeyEvent.KEYCODE_HOME)) {
		    			
		    			getActivity().finish();
		    			
		    			return true;

		    		} else {
		                return false;
		            }
		        }
		    });
        
        
		return view;
	}

	
	private void displayInvoiceHistory() {
		// TODO Auto-generated method stub
		lvInvoiceList = (ListView) view.findViewById(R.id.listView1);
		lvInvoiceList.clearTextFilter();
		ItemLocDS itemLocDs = new ItemLocDS(getActivity());
		arrayList = itemLocDs.getAllItemLoc();
		lvInvoiceList.setAdapter(new TestingAdapter(getActivity(), arrayList));
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:
			try {
				
				ItemLocDS ds=new ItemLocDS(getActivity());
				ds.createOrUpdateItemLoc(TestingAdapter.list);
				
			}catch (Exception e) {
				// TODO: handle exception
				Log.e("Exception", e.toString());
				
				}
			break;

		default:
			break;
		}
		
	}

	

}
